package cn.gene.timeyan.ams.webapi.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.gene.timeyan.ams.service.IAdminService;
import cn.gene.timeyan.pojo.ums.vo.UserVO;
import cn.gene.timeyan.ums.service.IUserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.time.LocalDateTime;

import java.time.ZoneId;
import java.util.*;

/**
 * 管理员   服务实现类*
 *
 * @author LWG
 * @date 2022/7/21 17:39
 * @since 1.0.0@gene.cn
 */
@Service
public class AdminServiceImpl implements IAdminService {

    @DubboReference
    private IUserService userService;

    @Override
    public Map<String, Object> members() {
        List<UserVO> userList = userService.list();
        int[] q = new int[12];
        //年（获取当前时间，取到当前的年份）
        Date date = new Date();
        int year = DateUtil.year(date);
        for (UserVO user : userList) {
            //LocalDateTime 转 Date
            LocalDateTime localDateTime = user.getCreateTime();
            Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
            Date time = Date.from(instant);
            Date createTime = time;
            //如果不是当前年就跳出本次循环
            if (DateUtil.year(createTime) != year) continue;
            //得到月份
            int month = DateUtil.month(createTime);

            if (month >= 0 && month < 13) {
                q[month]++;
            }
        }
        //纵坐标
        Map<String, Object> map = new HashMap<>();
        map.put("Y", Arrays.asList(q));
        //横坐标
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 12; i++) {
            list.add((1 + i) + "月");
        }
        map.put("X", list);
        //标题
        map.put("title", year + "年");

        //用户总人数
        map.put("totalUsers", userList.size());

        return map;
    }
}
